import { acceptHMRUpdate, defineStore } from "pinia";
import { useLocalShallowRef } from "@/framework/vue/hooks/state.ts";
import { shallowRef } from "vue";
import type { LocalProjectItem } from "@common/local-project.types.ts";
import { LocalProject } from "@/ipc/local-project.ts";

export const useProjectStore = defineStore("local-project", () => {
  const scanFolders = useLocalShallowRef<string[]>("local-project.scan.folders", []);
  const projects = shallowRef<LocalProjectItem[]>([]);

  const setScanFolders = (folders: string[]) => (scanFolders.value = folders);

  const loadProjects = async () => {
    projects.value = [];
    // console.log("scan folders =>", scanFolders.value);
    for (const folder of scanFolders.value) {
      const item = await LocalProject.scan(folder);
      projects.value = [...projects.value, item];
    }
  };

  return {
    scanFolders: scanFolders,
    setScanFolders: setScanFolders,
    projects: projects,
    loadProjects: loadProjects,
  };
});

if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useProjectStore, import.meta.hot));
}
